package com.mall507.fivepeaks.activity.community;

import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.google.gson.Gson;
import com.hzh.frame.comn.annotation.ViewInject;
import com.hzh.frame.comn.callback.HttpCallBack;
import com.hzh.frame.core.HttpFrame.BaseHttp;
import com.hzh.frame.ui.activity.BaseUI;
import com.hzh.frame.widget.rxbus.MsgEvent;
import com.hzh.frame.widget.rxbus.RxBus;
import com.mall507.comn.tools.UserTools;
import com.mall507.fivepeaks.R;
import com.mall507.fivepeaks.widget.xdialog.XRadioGroupDialog;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import io.reactivex.functions.Consumer;
import io.reactivex.functions.Predicate;

@Route(path = "/community/CommunityBusinessEnterUI")
public class CommunityBusinessEnterUI extends BaseUI {
    public static final String TAG="CommunityBusinessEnterUI";
    @ViewInject(R.id.name)
    EditText name;
    @ViewInject(R.id.phone)
    EditText phone;
    @ViewInject(R.id.bussiness)
    EditText bussiness;
    @ViewInject(R.id.area)
    TextView area;
    @ViewInject(R.id.address)
    TextView address;
    @ViewInject(R.id.submit)
    Button submit;
    @ViewInject(R.id.classify)
    TextView classify;
    
    public String lat = "0.0";
    public String lng = "0.0";
    public String addressName = "";

    private List<HashMap<String, Object>> areaList = new ArrayList<>();
    private List<HashMap<String, Object>> classifyList = new ArrayList<>();


    @Override
    protected void onCreateBase() {
        setContentView(R.layout.ui_bussiness_enter);
        getTitleView().setContent("填写入驻信息");
        address.setHorizontallyScrolling(true);
        choose();
        
        address.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String inName = name.getText().toString();
                String inPhone = phone.getText().toString();
                String inBussiness = bussiness.getText().toString();
                if (inName != null && inName.length() > 0){
                }else {
                    inName = "";
                }
                if (inPhone != null && inPhone.length() > 0){
                }else {
                    inPhone = "";
                }
                if (inBussiness != null && inBussiness.length() > 0){
                }else {
                    inBussiness = "";
                }
                ARouter.getInstance().build("/map/MapUI")
                        .withString("type","2")
                        .withString("name",inName)
                        .withString("phone",inPhone)
                        .withString("bussiness",inBussiness)
                        .withString("classify",classify.getText().toString())
                        .withString("classifyTag",classify.getTag(R.id.main_release_classify_value).toString())
                        .withString("area",area.getText().toString())
                        .withString("areaTag",area.getTag(R.id.main_release_area_value).toString())
                        .navigation(); //地图
                finish(); 
            }
        });
        submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (TextUtils.isEmpty(name.getText()) || TextUtils.isEmpty(phone.getText()) || TextUtils.isEmpty(bussiness.getText()) || TextUtils.isEmpty(addressName)){
                    alert("请完善信息再尝试提交");
                }else {
                    if (phone.getText().length()<11){
                        alert("请输入正确的联系电话");
                    }else{
                        if (bussiness.getText().toString().length() < 2){
                            alert("请输入商家名称(2至10字之间)");
                        }else {
                            enter();
                        }
                    }
                }
            }
        });
        
    }
    
    private void enter(){
        JSONObject params = new JSONObject();
        try {
            params.put("name", name.getText().toString());
            params.put("phoneNumber", phone.getText().toString());
            params.put("biz_name", bussiness.getText().toString());
            params.put("areaId", area.getTag(R.id.main_release_area_value).toString());
            params.put("address", address.getText().toString());
            params.put("lat", lat);
            params.put("lng", lng);
            params.put("id", classify.getTag(R.id.main_release_classify_value).toString());
        } catch (JSONException e) {
            e.printStackTrace();
        }
        
        BaseHttp.getInstance().query("Business/joinBusiness", params, new HttpCallBack() {
            @Override
            public void onSuccess(JSONObject response) {
                if (200 == response.optInt("code")){
                    onBackPressed();
                }
                alert(response.optString("msg"));
                super.onSuccess(response);
            }
        });
    }
    
    private void choose(){
        BaseHttp.getInstance().query("Release/getList", new HttpCallBack() {
            @Override
            public void onSuccess(JSONObject response) {
                if (response.optInt("code") == 200) {
                    JSONObject data = response.optJSONObject("data");
                    JSONArray area = data.optJSONArray("area");
                    chooseArea(area);
                   
                }
                super.onSuccess(response);
            }
        });

        BaseHttp.getInstance().query("Business/getCategory", new HttpCallBack() {
            @Override
            public void onSuccess(JSONObject response) {
                if (response.optInt("code") == 200) {
                    JSONArray data = response.optJSONArray("data");
                    chooseClassify(data);
                }
                super.onSuccess(response);
            }
        });
    }
    
    private void chooseArea(JSONArray params){
        if (params != null && params.length() > 0) {
            for (int i = 0; i < params.length(); i++) {
                JSONObject object = params.optJSONObject(i);
                HashMap<String, Object> map = new HashMap<>();
                map.put("name", object.optString("name"));
                map.put("value", object.optString("id"));
                areaList.add(map);
                if (i == 0) {//设置默认值,默认第一个为需要请求的值
                    area.setText(map.get("name").toString());
                    area.setTag(R.id.main_release_area_value, object.optString("id"));
                }
                setData();
                area.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        new XRadioGroupDialog().show(CommunityBusinessEnterUI.this, areaList, new XRadioGroupDialog.IRadioGroupDialogCallBack() {

                            @Override
                            public void confirm(HashMap<String, Object> selectedItem) {
                                area.setText(selectedItem.get("name").toString());
                                area.setTag(R.id.main_release_area_value, selectedItem.get("value").toString());
                            }

                            @Override
                            public void cancel() {

                            }
                        });
                    }
                });
            }
        }
    }

    private void chooseClassify(JSONArray params){
        if (params != null && params.length() > 0) {
            for (int i = 0; i < params.length(); i++) {
                JSONObject object = params.optJSONObject(i);
                HashMap<String, Object> map = new HashMap<>();
                map.put("name", object.optString("title"));
                map.put("value", object.optString("id"));
                classifyList.add(map);
                if (i == 0) {//设置默认值,默认第一个为需要请求的值
                    classify.setText(map.get("name").toString());
                    classify.setTag(R.id.main_release_classify_value, object.optString("id"));
                }
                setData();
                classify.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        new XRadioGroupDialog().show(CommunityBusinessEnterUI.this, classifyList, new XRadioGroupDialog.IRadioGroupDialogCallBack() {

                            @Override
                            public void confirm(HashMap<String, Object> selectedItem) {
                                classify.setText(selectedItem.get("name").toString());
                                classify.setTag(R.id.main_release_classify_value, selectedItem.get("value").toString());
                            }

                            @Override
                            public void cancel() {

                            }
                        });
                    }
                });
            }
        }
    }
    
    private void setData(){
        if (getIntent().getStringExtra("addressName") != null && getIntent().getStringExtra("addressName").length() > 0){
            lat = getIntent().getStringExtra("lat");
            lng = getIntent().getStringExtra("lng");
            addressName = getIntent().getStringExtra("addressName");
            address.setText(addressName);
            name.setText(getIntent().getStringExtra("name"));
            phone.setText(getIntent().getStringExtra("phone"));
            bussiness.setText(getIntent().getStringExtra("bussiness"));
            classify.setText(getIntent().getStringExtra("classify"));
            area.setText(getIntent().getStringExtra("area"));
            classify.setTag(R.id.main_release_classify_value, getIntent().getStringExtra("classifyTag"));
            area.setTag(R.id.main_release_area_value, getIntent().getStringExtra("areaTag"));
        }
    }
}
